package cn.wizzer.app.web.modules.controllers.platform.gift;

import cn.wizzer.app.user.modules.models.Gift;
import cn.wizzer.app.user.modules.services.GiftService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.app.web.commons.utils.StringUtil;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.filter.ParamFilter;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/gift/manager")
public class GiftController {
    private static final Log log = Logs.get();

    @Inject
    @Reference
    private GiftService giftService;

    @At("")
    @Ok("beetl:/platform/gift/index.html")
    @RequiresPermissions("gift.manage")
    public void index() {

    }

    @At("/add")
    @Ok("json")
    @RequiresPermissions("gift.manage.add")
    @SLog(tag = "添加礼物")
    public Object addDo(@Param("..") Gift gift) {
        gift.setOpBy(StringUtil.getPlatformUid());
        gift.setOpAt(System.currentTimeMillis()/1000);
        giftService.insert(gift);
        return Result.success();
    }

    @At("/edit")
    @Ok("json")
    @RequiresPermissions("gift.manage")
    @SLog(tag = "查询礼物信息")
    @Filters(@By(type = ParamFilter.class))
    public Object edit(@ApiParam(required = true, value = "id") @Param("id") String id) {
        Gift fetch = giftService.fetch(id);
        return Result.success().addData(fetch);
    }

    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("gift.manage.edit")
    @SLog(tag = "修改礼物信息")
    public Object editDo(@Param("..") Gift gift) {
        giftService.update(gift);
        return Result.success();
    }

    @At("/delete")
    @Ok("json")
    @RequiresPermissions("gift.manage.delete")
    @SLog(tag = "删除礼物")
    public Object delete(@Param("id") String id, HttpServletRequest req) {
        giftService.update(Chain.make("delFlag", true), Cnd.where("id", "=", id));
        return Result.success();
    }

    @At("/disable")
    @Ok("json")
    @RequiresPermissions("gift.manage.disable")
    @SLog(tag = "禁用")
    public Object disable(@Param("id") String id, @Param("disable") Boolean disable, HttpServletRequest req) {
        giftService.update(Chain.make("disabled", disable), Cnd.where("id", "=", id));
        return Result.success();
    }


    @At
    @Ok("json:full")
    @RequiresPermissions("gift.manage")
    public Object data(@Param("searchName") String searchName, @Param("searchKeyword") String searchKeyword, @Param("pageNumber") int pageNumber, @Param("pageSize") int pageSize) {
        Cnd cnd = Cnd.NEW();
        cnd.where().and("delFlag", "=", false);
        return Result.success().addData(giftService.listPage(pageNumber, pageSize, cnd));
    }


}
